﻿Imports System.Data.SqlClient
Public Class frmHangHoa
    Private strMaHH As String = ""
    Public booThanhCong As Boolean = False
    Sub New(Optional ByVal MaHH As String = "")

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        strMaHH = MaHH
    End Sub

    Private Sub frmHangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If strMaHH <> "" Then
            DocDuLieu(strMaHH)
        End If
        If strMaHH <> "" Then
            Me.txtMaHH.ReadOnly = True
        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        If validateInput() Then
            booThanhCong = CapNhat()
            If booThanhCong Then
                MsgBox("Lưu dữ liệu thành công!")
                strMaHH = Me.txtMaHH.Text
                Me.txtMaHH.ReadOnly = True
            Else
                MsgBox("Lưu dữ liệu thất bại!")
            End If
        End If

    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub txtMaHH_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaHH.Enter
        txtMaHH.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtMaHH_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaHH.Leave
        Me.txtMaHH.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Sub txtTenHH_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenHH.Enter
        txtTenHH.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtTenHH_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenHH.Leave
        Me.txtTenHH.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Function CapNhat() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = True
        Try
            myConn.Open()
            myComm.Connection = myConn
            If strMaHH <> "" Then
                strSql = "Update HangHoa set TenHH = @TenHH, DonViTinh = @DonViTinh, "
                strSql &= " DonGiaXuat = @DonGiaXuat, TonDK_SL = @TonDK_SL, TonDK_GT = @TonDK_GT, TonCK_SL = @TonCK_SL, TonCK_GT = @TonCK_GT where MaHH = @OriMaHH"
                myComm.Parameters.AddWithValue("@OriMaHH", strMaHH)
            Else
                strSql = "Insert into HangHoa(MaHH, TenHH, DonViTinh, DonGiaXuat, TonDK_SL, TonDK_GT, TonCK_SL, TonCK_GT) "
                strSql &= " values (@MaHH, @TenHH, @DonViTinh, @DonGiaXuat, @TonDK_SL, @TonDK_GT, @TonCK_SL, @TonCK_GT)"
                myComm.Parameters.AddWithValue("@MaHH", txtMaHH.Text.Trim())
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@TenHH", txtTenHH.Text.Trim())
            myComm.Parameters.AddWithValue("@DonViTinh", getEmptyValue(txtDVT.Text))
            myComm.Parameters.AddWithValue("@DonGiaXuat", getEmptyValue(txtDonGia.Text))
            myComm.Parameters.AddWithValue("@TonDK_SL", getEmptyValue(txtDKSL.Text))
            myComm.Parameters.AddWithValue("@TonDK_GT", getEmptyValue(txtDKGT.Text))
            myComm.Parameters.AddWithValue("@TonCK_SL", getEmptyValue(txtCKSL.Text))
            myComm.Parameters.AddWithValue("@TonCK_GT", getEmptyValue(txtCKGT.Text))
            myComm.ExecuteNonQuery()
        Catch ex As Exception
            result = False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Private Sub DocDuLieu(ByVal MaHH As String)
        Dim strSql As String = "Select * from HangHoa where MaHH = @MaHH"
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.SelectCommand.Parameters.AddWithValue("@MaHH", MaHH)
            myAdapter.Fill(myTable)
            If myTable.Rows.Count > 0 Then
                Me.txtMaHH.Text = myTable.Rows(0).Item("MaHH")
                Me.txtTenHH.Text = myTable.Rows(0).Item("TenHH")
                Me.txtDVT.Text = myTable.Rows(0).Item("DonViTinh")
                Me.txtDonGia.Text = myTable.Rows(0).Item("DonGiaXuat")
                Me.txtDKSL.Text = myTable.Rows(0).Item("TonDK_SL")
                Me.txtDKGT.Text = myTable.Rows(0).Item("TonDK_GT")
                Me.txtCKSL.Text = myTable.Rows(0).Item("TonCK_SL")
                Me.txtCKGT.Text = myTable.Rows(0).Item("TonCK_GT")
            Else
                Me.txtMaHH.Text = MaHH
                strMaHH = ""
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Function validateInput() As Boolean
        If Me.txtMaHH.Text.Trim = "" Then
            MsgBox(modError.HH_ERR_MAHH_REQUIRED, MsgBoxStyle.Information)
            txtMaHH.Focus()
            Return False
        Else
            If checkExist(Me.txtMaHH.Text.Trim, strMaHH) Then
                MsgBox(modError.HH_ERR_MAHH_EXISTED, MsgBoxStyle.Information)
                txtMaHH.Focus()
                Return False
            End If
        End If

        If Me.txtTenHH.Text.Trim = "" Then
            MsgBox(modError.HH_ERR_TENHH_REQUIRED, MsgBoxStyle.Information)
            txtTenHH.Focus()
            Return False
        End If

        If Me.txtDVT.Text.Trim = "" Then
            MsgBox(modError.HH_ERR_DVT_REQUIRED, MsgBoxStyle.Information)
            txtDVT.Focus()
            Return False
        End If

        If Me.txtDonGia.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtDonGia.Text) Then
            MsgBox(modError.HH_ERR_DGX_INPUT_WRONG, MsgBoxStyle.Information)
            txtDonGia.Focus()
            Return False
        End If
        If Me.txtDKSL.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtDKSL.Text) Then
            MsgBox(modError.HH_ERR_DKSL_INPUT_WRONG, MsgBoxStyle.Information)
            txtDKSL.Focus()
            Return False
        End If
        If Me.txtDKGT.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtDKGT.Text) Then
            MsgBox(modError.HH_ERR_DKGT_INPUT_WRONG, MsgBoxStyle.Information)
            txtDKGT.Focus()
            Return False
        End If
        If Me.txtCKSL.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtCKSL.Text) Then
            MsgBox(modError.HH_ERR_CKSL_INPUT_WRONG, MsgBoxStyle.Information)
            txtCKSL.Focus()
            Return False
        End If
        If Me.txtCKGT.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtCKGT.Text) Then
            MsgBox(modError.HH_ERR_CKGT_INPUT_WRONG, MsgBoxStyle.Information)
            txtCKGT.Focus()
            Return False
        End If
        Return True
    End Function

    Private Function checkExist(ByVal MaHH As String, Optional ByVal oriMaHH As String = "") As Boolean
        Dim strSql As String = "Select count(*) from HangHoa where MaHH = @MaHH "
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = False
        Try
            myConn.Open()
            If oriMaHH <> "" Then
                strSql &= " and MaHH <> @OrigMaHH"
                myComm.Parameters.AddWithValue("@OrigMaHH", oriMaHH)
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@MaHH", MaHH)
            myComm.Connection = myConn
            If myComm.ExecuteScalar() > 0 Then
                result = True
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Function getEmptyValue(ByVal value As String) As String
        If value.Trim = "" Then
            Return "0"
        End If
        Return value.Trim
    End Function
End Class